package com.dartfar.workflow.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.jbpm.api.identity.Group;
import org.jbpm.api.identity.User;
import org.jbpm.pvm.internal.identity.spi.IdentitySession;
import org.springframework.stereotype.Component;

import com.dartfar.base.dao.DaoUtil;
import com.dartfar.base.model.Person;
import com.dartfar.base.model.Role;
import com.dartfar.base.service.RoleService;
import com.dartfar.util.StringUtils;

@Component("identitySession")
public class IndentitySessionImpl implements IdentitySession {

	static Logger logger = Logger.getLogger(WorkflowTaskServiceImpl.class);
	
	@Resource
	private RoleService roleService;
	@Resource
	private DaoUtil<Person> personDaoUtil;
	
	@Override
	public String createGroup(String arg0, String arg1, String arg2) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void createMembership(String arg0, String arg1, String arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String createUser(String arg0, String arg1, String arg2, String arg3) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deleteGroup(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteMembership(String arg0, String arg1, String arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteUser(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Group findGroupById(String groupId) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		Role role = null;
		String hql = "from Role r where r.roleId = ?";
		
		List<?> roles = roleService.getListByIds(groupId);
		
		//pcDAOTemPlate.queryByHql(hql);
		if(null != roles && 0 != roles.size()) {
			role = (Role)roles.iterator().next();
		}
		logger.info("findGroupById: " + role.getName());
		return role;
	}

	@Override
	public List<Group> findGroupsByUser(String userId) {
		// TODO Auto-generated method stub
		List<Group> roles = new ArrayList<Group>();
		String hql = "from Person p where p.personId in ('" + userId + "')";
		logger.info("FindGroupsByUser hql: " + hql);
		List<?> persons = personDaoUtil.getList(hql);
		if(null != persons && 0 != persons.size()) {
			Person person = (Person)persons.iterator().next();
			Set<Role> roleSet = person.getRoles();
			if(null != roleSet && 0 != roleSet.size()) {
				roles.addAll(roleSet);
			} else {
				System.out.println("用户" + userId + "的角色为空!");
			}
			
		}
		
		logger.info("findGroupsByUserSize: " + roles.size());
		return roles;
	}

	@Override
	public List<Group> findGroupsByUserAndGroupType(String arg0, String arg1) {
		// TODO Auto-generated method stub
		System.out.println("根据userId和角色类型获取角色");
		return null;
	}

	@Override
	public User findUserById(String userId) {
		// TODO Auto-generated method stub
		Person person = null;
		String hql = "from Person p where p.personId in ('" + userId + "')";
		List<?> persons = personDaoUtil.getList(hql);
		if(null != persons && 0 != persons.size()) {
			person = (Person) persons.iterator().next();
		}
		
		logger.info("findUserById: " + person.getName());
		return person;
	}

	@Override
	public List<User> findUsers() {
		// TODO Auto-generated method stub
		List<User> users = null;
		String hql = "from Person";
		users.addAll(personDaoUtil.getList(hql));
		logger.info("findUsersSize: " + users.size());
		//users = (List<User>) pcDAOTemPlate.queryByHql(hql);
		return users;
	}

	@Override
	public List<User> findUsersByGroup(String groupId) {
		// TODO Auto-generated method stub
		List<User> persons = new ArrayList<User>();
		//String hql = "from Role r where r.roleCode = '" + groupId + "'";
		List<?> roles = roleService.getListByIds(groupId);
		//pcDAOTemPlate.queryByHql(hql);
		if(null != roles && 0 != roles.size()) {
			Role role = (Role)roles.iterator().next();
			persons.addAll(role.getPersons());
		}
		
		logger.info("findUsersByGroup: " + persons.size());
		return persons;
	}

	@Override
	public List<User> findUsersById(String... uIds) {
		// TODO Auto-generated method stub
		List<User> users = null;
		String ids = StringUtils.fromArrayToStr(uIds);
		String hql = "from Person p where p.personId in (" + ids + ")";
		users.addAll(personDaoUtil.getList(hql));
		logger.info("findUsersById: " + users.size());
		return users;
	}

}
